//导入vue
import Vue from 'vue'
//导入路由类
import Router from 'vue-router'
//全局注册路由
Vue.use(Router)
//导入登录组件
import Login from '@/views/login.vue'
//导入首页组件
import Home from '@/views/home.vue'
//导入welcome
import welcome from '@/components/welcome.vue'
//用户列表
import user from '@/components/User/user.vue'
//用户权限管理组件
import Rights from '@/components/rights/rights.vue'
//用户角色组件
import roles from '@/components/rights/roles.vue'
//商品分类组件
import goodsClass from '@/components/goods/goods_class.vue'


//制定路由匹配规则
const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { 
        path: '/home',
        component: Home,
        redirect:'/welcome',
        children:[
            {path:'/welcome',component:welcome},
            {path:'/users',component:user},
            {path:'/rights',component:Rights},
            {path:'/roles',component:roles},
            {path:'/categories',component:goodsClass},
        ]
     },
]
//new出路由实例对象
const router = new Router({ routes })
//实例对象添加路由守卫
router.beforeEach((to, from, next) => {
    //如果要前往登录页面则直接放行
    if (to.path == '/login') return next()
    //检查是否含有token
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    //如果有token则直接放行
    next(true)
})

//导出实例化路由对象
export default router
